package club.gskj.gulimall.order.config;


import club.gskj.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/***
 *  负责创建 Message Exchanhge Quene
 */
@Configuration
public class MyMqConfig {

    /**
     * 监听队列
     */
//    @RabbitListener(queues = {"order.release.order.queue"})
//    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期的订单信息，准备关闭订单 " + orderEntity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }

    /***
     * 容器中的这些组件都会自动创建(MQ中没有的话)
     *  死信队列
     *
     *  一旦创建好队列，MQ中只要有，即使后创建的相同队列的一些属性改变，mq的对应也不会改变
     */
    @Bean
    public Queue orderDelayQueue() {
        // 设置属性
        Map<String, Object> arguments = new HashMap<>();

        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order-release-order");
        arguments.put("x-message-ttl", 60000);

        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);
        return queue;
    }

    /**
     * 普通队列
     */
    @Bean
    public Queue orderReleaseQueue() {
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    /**
     * 交换机
     */
    @Bean
    public Exchange orderEventExchange() {
        // String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("order-event-exchange", true, false);
        return topicExchange;
    }

    /**
     * 绑定规则1
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        // Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments)

        Binding binding = new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order-create-order",
                null);
        return binding;
    }

    /**
     * 绑定规则2
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        Binding binding = new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order-release-order",
                null);
        return binding;
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * 绑定规则3 订单服务直接绑定 库存交换机，在订单取消状态后，发送消息到 库存队列，让库存再次查询并确认库存锁单信息，
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        Binding binding = new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order-release-other.#",
                null);
        return binding;
    }

    /**
     * 商品秒杀队列,(作用：消峰，消除大批流量峰值)
     */
    @Bean
    public Queue orderSeclikkQueue() {
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }

    /**
     * 秒杀队列绑定关系
     */
    @Bean
    public Binding orderSeclikkQueueBinding() {
        // Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments)
        Binding binding = new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
        return binding;
    }


}

